using System.Collections.Generic;
using Volo.Abp.Application.Dtos;

namespace Matrix.ServiceDiscovery.Models
{
    /// <summary>
    /// API响应包装器
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    public class ApiResponse<T>
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }

        /// <summary>
        /// 数据
        /// </summary>
        public T? Data { get; set; }

        /// <summary>
        /// 错误消息
        /// </summary>
        public string? Message { get; set; }

        /// <summary>
        /// 错误代码
        /// </summary>
        public string? ErrorCode { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        public System.DateTime Timestamp { get; set; } = System.DateTime.UtcNow;

        public static ApiResponse<T> SuccessResult(T data, string? message = null)
        {
            return new ApiResponse<T>
            {
                Success = true,
                Data = data,
                Message = message
            };
        }

        public static ApiResponse<T> ErrorResult(string message, string? errorCode = null)
        {
            return new ApiResponse<T>
            {
                Success = false,
                Message = message,
                ErrorCode = errorCode
            };
        }
    }

    /// <summary>
    /// 服务发现统计信息API模型
    /// </summary>
    public class ServiceStatisticsApiModel
    {
        /// <summary>
        /// 总服务数量
        /// </summary>
        public int TotalServices { get; set; }

        /// <summary>
        /// 活跃服务数量
        /// </summary>
        public int ActiveServices { get; set; }

        /// <summary>
        /// 总实例数量
        /// </summary>
        public int TotalInstances { get; set; }

        /// <summary>
        /// 健康实例数量
        /// </summary>
        public int HealthyInstances { get; set; }

        /// <summary>
        /// 按服务类型统计
        /// </summary>
        public Dictionary<string, int> ServicesByType { get; set; } = new();

        /// <summary>
        /// 按健康状态统计
        /// </summary>
        public Dictionary<string, int> InstancesByHealthStatus { get; set; } = new();

        /// <summary>
        /// 按服务分组统计
        /// </summary>
        public Dictionary<string, int> ServicesByGroup { get; set; } = new();

        /// <summary>
        /// 平均响应时间
        /// </summary>
        public double AverageResponseTimeMs { get; set; }

        /// <summary>
        /// QPS（每秒查询数）
        /// </summary>
        public double QueriesPerSecond { get; set; }

        /// <summary>
        /// 统计时间
        /// </summary>
        public System.DateTime StatisticsTime { get; set; } = System.DateTime.UtcNow;
    }

    /// <summary>
    /// 服务健康检查API模型
    /// </summary>
    public class ServiceHealthCheckApiModel
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; } = string.Empty;

        /// <summary>
        /// 实例ID
        /// </summary>
        public string InstanceId { get; set; } = string.Empty;

        /// <summary>
        /// 实例地址
        /// </summary>
        public string Address { get; set; } = string.Empty;

        /// <summary>
        /// 健康状态
        /// </summary>
        public string HealthStatus { get; set; } = string.Empty;

        /// <summary>
        /// 响应时间（毫秒）
        /// </summary>
        public int ResponseTimeMs { get; set; }

        /// <summary>
        /// 检查时间
        /// </summary>
        public System.DateTime CheckTime { get; set; } = System.DateTime.UtcNow;

        /// <summary>
        /// 错误消息
        /// </summary>
        public string? ErrorMessage { get; set; }
    }

    /// <summary>
    /// 服务路由信息API模型
    /// </summary>
    public class ServiceRouteApiModel
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        public string ServiceName { get; set; } = string.Empty;

        /// <summary>
        /// 路由路径
        /// </summary>
        public string RoutePath { get; set; } = string.Empty;

        /// <summary>
        /// 目标地址
        /// </summary>
        public List<string> TargetAddresses { get; set; } = new();

        /// <summary>
        /// 负载均衡策略
        /// </summary>
        public string LoadBalancingStrategy { get; set; } = string.Empty;

        /// <summary>
        /// 权重
        /// </summary>
        public Dictionary<string, int> Weights { get; set; } = new();

        /// <summary>
        /// 元数据
        /// </summary>
        public Dictionary<string, string> Metadata { get; set; } = new();
    }
}